package com.yhd.admin.web.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.yhd.admin.bean.SysMenu;
import com.yhd.admin.service.SysMenuService;
import com.yhd.admin.utils.EasyuiTreeNode;

@Controller
@RequestMapping("/main")
public class MainController {
	@Autowired
	private SysMenuService sysMenuService;
	
	@RequestMapping()
	public String pageMain(){
		return "main";
	}
	
	//异步加载菜单
	@RequestMapping("/tree")
	public @ResponseBody Object getMenuByPid(@RequestParam(defaultValue="0") int id){
		List<SysMenu> menus = sysMenuService.getAsyncChildrenByPid(id);
		
		List<EasyuiTreeNode> nodes = new ArrayList<EasyuiTreeNode>();
		//对象转换
		for(SysMenu m : menus){
			EasyuiTreeNode node = ToEasyuiTreeNode(m);
			nodes.add(node);
		}
		return nodes;
	}
	
	private EasyuiTreeNode ToEasyuiTreeNode(SysMenu menu){
		EasyuiTreeNode node = new EasyuiTreeNode();
		node.setId(menu.getId());
		node.setText(menu.getName());
		//判断是否叶子节点
		if(menu.getUrl()!=null){
			node.getAttributes().put("url", menu.getUrl());
			node.setState("open");
		}
		return node;
	}
}
